package com.ls.uav_backend.service.impl;

import com.ls.uav_backend.mapper.CropGrowthIndicatorsMapper;
import com.ls.uav_backend.model.entity.CropGrowthIndicators;
import com.ls.uav_backend.model.vo.crop.*;
import com.ls.uav_backend.service.CropGrowthIndicatorsService;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 农作物生长指标表 服务层实现。
 *
 * @author <a href="https://gitee.com/Linkating">LinSkating</a>
 */
@Service
public class CropGrowthIndicatorsServiceImpl extends ServiceImpl<CropGrowthIndicatorsMapper, CropGrowthIndicators>
        implements CropGrowthIndicatorsService {

    @Resource
    CropGrowthIndicatorsMapper cropGrowthIndicatorsMapper;


    /**
     * 获取指定田地ID作物生长分析数据
     * @param fieldId 田地ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return GrowthAnalysisResponseVO 包含作物生长分析数据的响应对象
     */
    @Override
    public GrowthAnalysisResponseVO getGrowthAnalysisData(Long fieldId, Date startTime, Date endTime) {
        // 构建查询条件，根据田地ID和记录时间范围查询作物生长指标数据
        QueryWrapper queryWrapper = this.queryChain()
                .eq(CropGrowthIndicators::getFieldId, fieldId)  // 设置田地ID条件
                .between(CropGrowthIndicators::getRecordTime, startTime, endTime)  // 设置时间范围条件
                .toQueryWrapper();
        // 执行查询获取作物生长指标数据列表
        List<CropGrowthIndicators> resList = this.list(queryWrapper);
        // 创建生长分析响应对象
        GrowthAnalysisResponseVO growthAnalysisResponseVO = new GrowthAnalysisResponseVO();
        // 获取并初始化各种生长指标的VO对象
        LeavesCountVO leavesCountData = growthAnalysisResponseVO.getLeavesCountData();
        LeavesSizeVO leavesSizeData = growthAnalysisResponseVO.getLeavesSizeData();
        PlantHeightVO plantHeightData = growthAnalysisResponseVO.getPlantHeightData();
        LeavesColorDetailVO leavesColorDetailData = growthAnalysisResponseVO.getLeavesColorDetailData();
        // 设置各个VO对象的田地ID
        leavesCountData.setFieldId(fieldId);
        leavesSizeData.setFieldId(fieldId);
        plantHeightData.setFieldId(fieldId);
        leavesColorDetailData.setFieldId(fieldId);
        growthAnalysisResponseVO.setFieldId(fieldId);
        // 遍历查询结果，填充各个生长指标数据
        for (CropGrowthIndicators cropGrowthIndicators : resList) {
            LocalDateTime recordTime = cropGrowthIndicators.getRecordTime();
            List<LocalDateTime> recordTimes = leavesCountData.getRecordTimes();
            recordTimes.add(recordTime);
            // leavesCountData
            leavesCountData.setRecordTimes(recordTimes);
            List<Integer> leafTotalCounts = leavesCountData.getLeafTotalCounts();
            Integer leafTotalCount = cropGrowthIndicators.getLeafTotalCount();
            leafTotalCounts.add(leafTotalCount);
            leavesCountData.setLeafTotalCounts(leafTotalCounts);
            // leavesSizeData
            leavesSizeData.setRecordTimes(recordTimes);
            List<Double> leafAvgSizes = leavesSizeData.getLeafAvgSizes();
            BigDecimal leafAvgSize = cropGrowthIndicators.getLeafAvgSize();
            leafAvgSizes.add(leafAvgSize.doubleValue());
            leavesSizeData.setLeafAvgSizes(leafAvgSizes);
            // plantHeightData
            plantHeightData.setRecordTimes(recordTimes);
            List<Double> plantHeights = plantHeightData.getPlantHeights();
            BigDecimal plantHeight = cropGrowthIndicators.getPlantHeight();
            plantHeights.add(plantHeight.doubleValue());
            plantHeightData.setPlantHeights(plantHeights);
            // leavesColorDetailData
            leavesColorDetailData.setRecordTimes(recordTimes);
            List<Double> leafAlbinoRatios = leavesColorDetailData.getLeafAlbinoRatios();
            BigDecimal leafAlbinoRatio = cropGrowthIndicators.getLeafAlbinoRatio();
            leafAlbinoRatios.add(leafAlbinoRatio.doubleValue());
            leavesColorDetailData.setLeafAlbinoRatios(leafAlbinoRatios);
            List<Double> leafDarkGreenRatios = leavesColorDetailData.getLeafDarkGreenRatios();
            BigDecimal leafDarkGreenRatio = cropGrowthIndicators.getLeafDarkGreenRatio();
            leafDarkGreenRatios.add(leafDarkGreenRatio.doubleValue());
            leavesColorDetailData.setLeafDarkGreenRatios(leafDarkGreenRatios);
            List<Double> leafWitheredRatios = leavesColorDetailData.getLeafWitheredRatios();
            BigDecimal leafWitheredRatio = cropGrowthIndicators.getLeafWitheredRatio();
            leafWitheredRatios.add(leafWitheredRatio.doubleValue());
            leavesColorDetailData.setLeafWitheredRatios(leafWitheredRatios);
            List<Double> leafYellowingRatios = leavesColorDetailData.getLeafYellowingRatios();
            BigDecimal leafYellowingRatio = cropGrowthIndicators.getLeafYellowingRatio();
            leafYellowingRatios.add(leafYellowingRatio.doubleValue());
            leavesColorDetailData.setLeafYellowingRatios(leafYellowingRatios);
        }
        return growthAnalysisResponseVO;
    }

    @Override
    public Set<Integer> getFieldIdSet() {
        List<CropGrowthIndicators> list = this.queryChain()
                .select(CropGrowthIndicators::getFieldId)
                .list();
        return list.stream().map(CropGrowthIndicators::getFieldId).collect(Collectors.toSet());
    }

    @Override
    public MaturityRateVO getMaturityRateData() {
        List<CropGrowthIndicators> cropGrowthIndicators = cropGrowthIndicatorsMapper.selectLatestByFieldId();
        cropGrowthIndicators.sort((o1, o2) -> o2.getMaturityUnripeRatio().compareTo(o1.getMaturityUnripeRatio()));
        MaturityRateVO maturityRateVO = new MaturityRateVO();
        int count = 0;
        for (CropGrowthIndicators item : cropGrowthIndicators) {
            if (count == 6) break;
            maturityRateVO.getFieldIds().add(item.getFieldId());
            maturityRateVO.getFieldNames().add(item.getFieldId() + "号田");
            maturityRateVO.getMaturityRipeRatios().add(item.getMaturityRipeRatio().doubleValue());
            maturityRateVO.getMaturityUnripeRatios().add(item.getMaturityUnripeRatio().doubleValue());
            maturityRateVO.getMaturitySemiRipeRatios().add(item.getMaturitySemiRipeRatio().doubleValue());
            count++;
        }
        return maturityRateVO;
    }

    @Override
    public FieldLeafColorSumVO selectLatestColor() {
        CropGrowthIndicators cropGrowthIndicators = cropGrowthIndicatorsMapper.selectLatestColor();
        int leafTotalCount = cropGrowthIndicators.getLeafTotalCount();
        double leafDarkGreenRatio = cropGrowthIndicators.getLeafDarkGreenRatio().doubleValue();
        double leafYellowingRatio = cropGrowthIndicators.getLeafYellowingRatio().doubleValue();
        double leafWitheredRatio = cropGrowthIndicators.getLeafWitheredRatio().doubleValue();
        double leafAlbinoRatio = cropGrowthIndicators.getLeafAlbinoRatio().doubleValue();
        FieldLeafColorSumVO result = new FieldLeafColorSumVO();
        result.setSumLeafDarkGreenCount(leafTotalCount * leafDarkGreenRatio);
        result.setSumLeafYellowingCount(leafTotalCount * leafYellowingRatio);
        result.setSumLeafWitheredCount(leafTotalCount * leafWitheredRatio);
        result.setSumLeafAlbinoCount(leafTotalCount * leafAlbinoRatio);
        return result;
    }


}
